package tree;

/**
 * Created with IntelliJ IDEA.
 * Description: 108. 将有序数组转换为二叉搜索树
 * User: Li_yizYa
 * Date: 2025/6/11
 * Time: 21:33
 */
public class Solution36 {
    public TreeNode sortedArrayToBST(int[] nums) {
        if (nums == null || nums.length == 0) {
            return null;
        }
        return function(nums, 0, nums.length - 1);
    }
    private TreeNode function(int[] nums, int left, int right) {
        if (left > right) {
            return null;
        }
        int mid = (left + right) / 2;
        TreeNode root = new TreeNode(nums[mid]);
        root.left = function(nums, left, mid - 1);
        root.right = function(nums, mid + 1, right);

        return root;
    }
}
